<template>
  <basic-dialog :visible="show"
                title="权限设置"
                :isShowInnerLoading="isShowInnerLoading"
                min-width="800px"
                isShowClose
                :isShowBasicFooter="true"
                @close="$emit('close')"
                @submit="submit">
    <div class="padding-20">
        <div class="flex items-center" v-if="shapeOptions.length > 0">
          <div class="set-header">业务范围：</div>
          <div>
            <q-radio v-model="shape" :val="v.value" :label="v.label"  v-for="(v, index) in shapeOptions" :key="index"/>
          </div>
        </div>
      <div v-if="isShowSimple">
        <div class="set-header">数据权限：</div>
        <div class="flex items-center justify-between  full-width padding-l-20 ptree-header padding-r-20">
          <div>生产组织名称</div>
          <div>生产组织层级</div>
        </div>
        <div class="scrollbar overflow-auto" style=" height: 400px" >
          <q-tree class="col-12 col-sm-6"
                  v-if="simple.length"
                  :nodes="simple"
                  ref="qTree"
                  node-key="id"
                  :tick-strategy="tickStrategy"
                  :ticked.sync="ticked"
                  noNodesLabel="暂无数据.."
                  control-color="#999999"
                  style="padding-left: 20px; padding-right: 20px; font-size: 16px"
                  default-expand-all>
            <template v-slot:default-header="prop">
              <div class="row items-center justify-between  full-width padding-r-20">
                <div>{{prop.node.label}}</div><div>{{prop.node.leveldesc}}</div>
              </div>
            </template>
          </q-tree>
      </div>
      </div>
    </div>
  </basic-dialog>
</template>

<script>
import _ from 'lodash'
import { getTreeTenant } from '../../boot/program-v2/machine'
import { defaultSchemesClearer } from '../../assets/js/util'
import { postPrivilege, saveBusinessPermissionItem } from '../../boot/system/role-menu'
export default {
  name: 'role-set-dialog',
  components: {
  },
  props: {
    show: Boolean,
    setOptions: Array,
    roleId: String,
    tab: String,
    roleDataDialog: Object
  },
  data () {
    return {
      ticked: [],
      tickStrategy: 'leaf',
      simple: [],
      shapeOptions: [],
      allSchemasData: [],
      shape: null,
      isShowInnerLoading: false
    }
  },
  watch: {
    show (newPro) {
      if (newPro) {
        console.log(this.setOptions)
        this.shapeOptions = this.roleDataDialog.businessAuthorityOpt || []
        this.shape = this.roleDataDialog.businessAuthority
        this.ticked = []
        this.simple = []
        this.getInfo()
      }
    }
  },
  computed: {
    isShowSimple () {
      return this.setOptions.includes(this.roleDataDialog.menuId)
    }
  },
  methods: {
    getInfo () {
      this.isShowInnerLoading = true
      getTreeTenant().then(res => {
        let folder = defaultSchemesClearer.clear(res.data)
        this.simple = _.cloneDeep(folder)
        this.allSchemasData = this.simple.map((item) => {
          return this.setArrItem(item)
        }).flat()
        const roleMenu = this.roleDataDialog.isAll ? this.allSchemasData : _.get(this.roleDataDialog.roleMenuData, 'schemas', [])
        this.ticked = roleMenu.map(v => v.schemaId)
        this.isShowInnerLoading = false
      })
    },
    setArrItem (item) {
      if (item.children) {
        return item.children.map(v => this.setArrItem(v)).flat()
      } else {
        return {
          ...item,
          schemaId: item.id
        }
      }
    },
    async submit () {
      if (_.isEmpty(this.ticked) && this.isShowSimple) {
        this.$message.warning('请选择生产组织名称')
        return
      }
      const schemaData = this.ticked.map(v => {
        return {
          schemaId: v,
          schemaLevel: 2
        }
      })
      const params = {
        clientType: this.tab,
        roleId: this.roleId,
        viewPrivilegeId: this.roleDataDialog.menuId,
        isAll: this.allSchemasData.length === this.ticked.length,
        data: schemaData
      }
      this.isShowInnerLoading = true
      if (!_.isNil(this.shape)) {
        await saveBusinessPermissionItem(this.roleId, this.tab, this.shape)
      }
      let ret
      if (this.isShowSimple) {
        ret = await postPrivilege(params)
      }
      ret?.success && this.$message.success('设置成功')
      this.$emit('close')
      this.isShowInnerLoading = false
    }
  }
}
</script>

<style scoped lang="scss">
.set-header{
  height: 40px;
  line-height: 40px;
  font-weight: bold;
  font-size: 16px;
  color:#000000;
}
.ptree-header{
  height: 50px;
  background: #F3F3F3;
}
</style>
